﻿<?php 
class Formermodel extends CI_Model{
    function __construct(){
        parent::__construct();
		$this->load->database();
		$this->load->library(array('session','pagination'));
		$this->load->helper('url');

    }
	

	function get_category(){
		$sql="select * from category";
		$query=$this->db->query($sql);
		return $query->result_array();

	}
	
	
	
	
	
	
	
	
	
	
	/*
	*
	*
	*文章操作
	*
	*
	*
	*/
	function get_article($lim,$perpage){
		// $sql="select * from article";
		// $query=$this->db->query($sql);
		// return $query->result_array();
		//$id=$this->uri->segment(3);
		$sql="select * from article order by time desc limit $lim,$perpage ";
		$query=$this->db->query($sql);
		return $query->result_array();
		
		
	}
	function get_hot_article(){
		$sql="select * from article order by num desc limit 0,5 ";
		$query=$this->db->query($sql);
		return $query->result_array();
		
	}
	function get_rand_article(){
		$sql="select * from article order by rand() limit 0,5";
		$query=$this->db->query($sql);
		return $query->result_array();
		
	}
	
	
	
	function read_article(){
		$id=$this->uri->segment(3);
		$sql="select * from article where id='$id'";
		$query=$this->db->query($sql);
		$row=$query->row();
		$num=$row->num;
		$num+=1;
		$this->db->where("id",$id);
		$data=array("num"=>$num);
		$this->db->update("article",$data);
		return $query->row(); //相当于又执行了一次select 查询

	}
	
	function get_article_rows($cid){
		$sql="select * from article where cid='$cid'";
		$query=$this->db->query($sql);
		return $query->num_rows();

	}
	function get_article_totalrows(){
		$sql="select * from article";
		$query=$this->db->query($sql);
		return $query->num_rows();
		
	}
 	function get_article_list($cid,$lim,$perpage){
		$query=$this->db->query("select * from article where cid='$cid' order by settop desc,time limit $lim,$perpage");
		return $query->result_array();
		
	} 
	
	
	
	/*
	*
	*
	*管理评论
	*
	*
	*
	*/
	function addcomment($content){
		$id=$this->uri->segment(3);
		//$cid=>$this->uri->segment(4);
		$time=date("Y-m-d H:i:s");
		$name=$this->input->post('name');
		$email=$this->input->post('email');
		$data=array(
		    'name'=>$name,
			'email'=>$email,
			'content'=>$content,
			'id'=>$id,
			'time'=>$time
			
		);
		$query=$this->db->insert("comment",$data);
		if($query){
		    return 1;
		}else{
			return 0;
		}
	
	}
	function getcomment($id){
		
		$sql="select * from comment where id='$id'";
		$query=$this->db->query($sql);
		return $query->result_array();
	}
	function getcomment_num($id){ //统计数据库评论的条数
		$sql="select * from comment where id='$id'";
		$query=$this->db->query($sql);
		return $query->num_rows();
		
	}

	
	
	
	//实现站内搜索
	function search($c){
		//$c=$this->input->post('sstxt');
		//$c=trim($c);
		//$c=str_replace("'",'m',$c);
		$sql=" select * from  article  where author like binary '%$c%' or title like binary '%$c%' or content like binary '%$c%' or time like binary '%$c%' or class like binary '%$c%'";
		$query=$this->db->query($sql);
		return $query->result_array();
		
	}
	//文件下载
	function getdownloadlist(){
	$query=$this->db->query('select * from file_img');
	return $query->result_array();
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}




































?>